const express = require('express');
const { ApolloServer, gql } = require('apollo-server-express');

const libraries = [
  {
    branch: 'downtown'
  },
  {
    branch: 'riverside'
  },
];

// The branch field of a book indicates which library has it in stock
const books = [
  {
    // author:"1",
    title: 'The Awakening',
    branch: 'riverside',
  },
  {
    // author:"2",
    title: 'City of Glass',
    branch: 'downtown'
  },
];
const author = [
  {
    name:'Kate Chopin',
    branch: 'riverside'
  }
  ,{
    name:'Paul Auster',
    branch: 'downtown'
  }
]
// Schema definition
const typeDefs = gql`

# A library has a branch and books
  type Library {
    branch: String!
    books: [Book!]
  }

  # A book has a title and author
  type Book {
    title: String!
    author: Author!
  }

  # An author has a name
  type Author {
    name: String!
  }

  # Queries can fetch a list of libraries
  type Query {
    libraries: [Library]
    books(title:String!):[Book]
    authors:[Author]
  }
  type Mutation {
    # This mutation takes id and email parameters and responds with a User
    updatebooks(title:String,branch:String!,name:String!): Book
    updateauthors(name:String!): Author

  }
  
`;

// Resolver map
const resolvers = {
  Query: {
    libraries(parent,args) {

      // Return our hardcoded array of libraries
      return libraries;
    },
    books(parent,args){
      return books.filter(book =>args.title ===book.title
      );
    },
    authors(){
      return author
    }
    
  },
  Mutation:{
    updatebooks:(parent,args)=>{
      const books1 = {
        title:args.title,
        branch:args.branch
      }
      const authors1 = {
        name:args.name,
        branch:args.branch
      }
      if (books){
        books.push(books1)//这里的值将会改变author
      }
      if (author){
        author.push(authors1)
      }
      console.log(books1)
    return {...books1,author:authors1}
    },
    updateauthors:(parent,args)=>{
      const authors2 ={
        name:args.name
      }
      if (author){
        author.push(authors2)
      }
    return authors2

    }
  },
  Library: {
    books(parent) {

      // Filter the hardcoded array of books to only include
      // books that are located at the correct branch
  
      return books.filter(book => book.branch === parent.branch);
      }
    },
  Book: {

    // The parent resolver (Library.books) returns an object with the
    // author's name in the "author" field. Return a JSON object containing
    // the name, because this field expects an object.
    author(parent) {
      let res = author.filter(s=>{
        if (s.branch ===parent.branch){
          return true
        }
      })[0]
      return res
      ;
    }
    // author(parent){
    //   return {name:parent.author}
    // }
  }

  // Because Book.author returns an object with a "name" field,
  // Apollo Server's default resolver for Author.name will work.
  // We don't need to define one.
};


// Pass schema definition and resolvers to the ApolloServer constructor
const server = new ApolloServer({ typeDefs, resolvers,tracing: true });

const app = express();
server.applyMiddleware({ app });
app.listen({ port: 4000 }, () =>
  console.log('Now browse to http://localhost:4000' + server.graphqlPath)
);